package com.shenbianyun.lijianjin;



import com.alibaba.fastjson.JSONObject;
import com.shenbianyun.bosskgpay.constant.MerInfoConstant;
import com.shenbianyun.bosskgpay.entity.RequestMessage;
import com.shenbianyun.bosskgpay.entity.pay.LijianjinPayItemsDTO;
import com.shenbianyun.bosskgpay.utils.*;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;


public class LijianjinSendMoneyTest extends AbstractJavaSamplerClient {


    public final String CHARSET_UTF_8 = "UTF-8";
    public final String API_VERSION = "V1.0";
    //测试环境
    //public static final String OMS_TEST_URL = "http://192.168.40.85:8907/clientapi/clientBusiness/common";
//    public final Long MER_ID = 1702976946849291L;
//    public final String MER_PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMhyR4I/yTdDrrI6C7HhHEH20HtbkF+wrFWoG7Rk18TatN7L6rLcW3mV3fPZUM07W7pKEGVz01grrl/OoKHbKWAEm7n+WdRXsBJ+VinVSdI0IynquDjh6qTUM8gBrPaU3KpwSdVqdY51foQw4mr7h93Bq8W6GOkgBiosQYd2yYF5AgMBAAECgYEAk6ZyfEQmIPpPLePKB45tdT2slqF8+K+4UiRRvBDoS45a2x7UVEoza510cBItDfBighfaehDWpkUO3m3jLza0A0PHmbryNsyrw0HSLiPJVaf1/wP2jvI94veXIlL1ttyKsq1kSVhgUuRS2anq/IXdaF2G1NE/rmeGatCPg0kcp7ECQQDinjgD1z8D0y+7duL8HD3+sCL94VXlGrj2DrzI75zXyuf4CBqSSRSX1lfy3HHjw5alVnmUqVQPyIK/P50JWJxlAkEA4m9iy1czgowK7DTUWOGXMVobzZdjvS168FllIJxrj3dhAvoT6MduotUoZ3e+2e9F3AoGiLGh4+NM7lmciNCthQJAb33uCx2z9/rHyhA0nlp6uboIjSV2r+/UveIESUtxu80tER9Ti1L/GbCvtmF0et04WYHQ8S3ZmKZKhTMT3IxG7QJAMoRBGTZaIozVvv7GRRIHJhaBT2uv77ueytJW0mCzSm2KshxSpsMfdqpxeivn4z0Ta9S8wnBGus2yExbg6lEL9QJBAJG8lqFuPfpzHtoCd7Xbv0GWJ+K/modlWPWbxANJQw9OL2sqQ2Was/Ipflyggu+gDDkkhePe77YkSdlAJxVNbzY=";
//    public final String MER_PUBLIC_KEY="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIckeCP8k3Q66yOgux4RxB9tB7W5BfsKxVqBu0ZNfE2rTey+qy3Ft5ld3z2VDNO1u6ShBlc9NYK65fzqCh2ylgBJu5/lnUV7ASflYp1UnSNCMp6rg44eqk1DPIAaz2lNyqcEnVanWOdX6EMOJq+4fdwavFuhjpIAYqLEGHdsmBeQIDAQAB";
//    public final String INTER_KEY = "930A7CE165CB2D27F62A34862A397618";


    //生产环境
    public final String OMS_TEST_URL = "http://uatapi.merchant.serviceshare.com/clientapi/clientBusiness/common";
//    public final String MER_PRIVATE_KEY = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJ/HPaLyoFQq1TqOa6G97JaN7EmW3+/gZbHHOUjyxUCwhiHorqOGvYze/ovkL2fThdE2tjaau/JiS/6ueMDnIAWEOBs7ZgqbTn3oKlJZUHOgld8ZRKl0wYl88itCIerrZFkp8L04LtGpn82AkJb06Tm1uNZMd7mHnCaIWdcZ/O5BAgMBAAECgYAQzXch57oMn+SEn+2ZuMtkmdA7fO5X35VSOM1BaqZrwySX9ExJTiYo5lqGg5yooon0EnjfDS6usr+jLN3uC2BGiM+nmfVPG5BHa2oBINeTcZgI+pIEW56Pgh8Xu7rqs2XDsOF72gGapjYbr8tZyJGanWKXrM+aqJoixrv0m9dssQJBAPgNua1aBpZDFzkJXQF9jmH4m+zG0A3LRSWEuVzJLZCV1eP78NgDwE+pqcRRsNVOGY0wmOB0i3fjTxOE/8Wfc7cCQQCk5ZL4sTHl3a1FI8LejtYL0el8BKY7BeZqM0gwbb9ZTbq+QsZNirpKoTsj1yIA34B5dl8F0UcDzmiVttpPA93HAkBPbFWfY8voIOM2qADXkVQdmnSSg7XqNMOXF8AiLWghe1hY/zpGcC0LVM++xElSgwc7ZewNMkPVxzpyjYs3w65RAkBtOnWErzuyXIkuR2YKLuuyoLXdJOdIKbxblOS4z7n02fxdk7XwH3kPfoXCU/t+QI7F5pdDL0GvT8cPhTRPKux5AkA7pKhfYwnaW5sQKEjKIZsBa6E9fHGUjeX0EcSl+ztEpOG/DBB4dNqkZBS+SuYARiWxfUsWNnc2lf9dlABWzX0S";
//    public final String MER_PUBLIC_KEY="MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIckeCP8k3Q66yOgux4RxB9tB7W5BfsKxVqBu0ZNfE2rTey+qy3Ft5ld3z2VDNO1u6ShBlc9NYK65fzqCh2ylgBJu5/lnUV7ASflYp1UnSNCMp6rg44eqk1DPIAaz2lNyqcEnVanWOdX6EMOJq+4fdwavFuhjpIAYqLEGHdsmBeQIDAQAB";
//    public final Long MER_ID = 1623945517108708L;
//    public final String INTER_KEY = "8193717818841741B1D303615DF36DE1";

    //参数化
    private Long merID =1702976946849291L;
    private String merPrivateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMhyR4I/yTdDrrI6C7HhHEH20HtbkF+wrFWoG7Rk18TatN7L6rLcW3mV3fPZUM07W7pKEGVz01grrl/OoKHbKWAEm7n+WdRXsBJ+VinVSdI0IynquDjh6qTUM8gBrPaU3KpwSdVqdY51foQw4mr7h93Bq8W6GOkgBiosQYd2yYF5AgMBAAECgYEAk6ZyfEQmIPpPLePKB45tdT2slqF8+K+4UiRRvBDoS45a2x7UVEoza510cBItDfBighfaehDWpkUO3m3jLza0A0PHmbryNsyrw0HSLiPJVaf1/wP2jvI94veXIlL1ttyKsq1kSVhgUuRS2anq/IXdaF2G1NE/rmeGatCPg0kcp7ECQQDinjgD1z8D0y+7duL8HD3+sCL94VXlGrj2DrzI75zXyuf4CBqSSRSX1lfy3HHjw5alVnmUqVQPyIK/P50JWJxlAkEA4m9iy1czgowK7DTUWOGXMVobzZdjvS168FllIJxrj3dhAvoT6MduotUoZ3e+2e9F3AoGiLGh4+NM7lmciNCthQJAb33uCx2z9/rHyhA0nlp6uboIjSV2r+/UveIESUtxu80tER9Ti1L/GbCvtmF0et04WYHQ8S3ZmKZKhTMT3IxG7QJAMoRBGTZaIozVvv7GRRIHJhaBT2uv77ueytJW0mCzSm2KshxSpsMfdqpxeivn4z0Ta9S8wnBGus2yExbg6lEL9QJBAJG8lqFuPfpzHtoCd7Xbv0GWJ+K/modlWPWbxANJQw9OL2sqQ2Was/Ipflyggu+gDDkkhePe77YkSdlAJxVNbzY=";
    private String interKey = "930A7CE165CB2D27F62A34862A397618";

    public static LijianjinPayItemsDTO payItemsDTO = null;


    public String runTestUserSignQueryService(long MER_ID,String MER_PRIVATE_KEY,String INTER_KEY) throws Exception {
        //公共参数
        RequestMessage rm = new RequestMessage();
        //接口地址
        rm.setFunCode("8001");
        rm.setMerId(MER_ID);
        rm.setVersion(API_VERSION);
        Long id = SnowflakeIdGeneratorFactory.getInstance().nextId();
        rm.setReqId("test"+id);

        //业务参数：
        payItemsDTO = new LijianjinPayItemsDTO();
        payItemsDTO.setProviderId(456758L);//43210L
        payItemsDTO.setAmt(20L);
        payItemsDTO.setAccountType("BASIC");
        payItemsDTO.setProductSubType("MONEY");//(MONEY:零钱,REDPACK:红包,MONEY_ALIPAY:支付宝零钱)
        payItemsDTO.setMerOrderId("order"+id);
        payItemsDTO.setPayeeAcc("o48_Ct_ay6ngT00a5vmdtUPY3tUQ");//支付宝userid：2088312584136032  微信openid：o48_Ct_ay6ngT00a5vmdtUPY3tUQ
        payItemsDTO.setPayeeName("李正午");//微信姓名：李正午
//        payItemsDTO.setMemo("支付宝转账备注-其他");
//        payItemsDTO.setRedPackName("支付宝标题-其他");
//        payItemsDTO.setWishing("红包祝福语红包祝福语红包祝福语红包祝福语红包祝福语红包祝福语红包");
//        payItemsDTO.setSceneType("AWARD");
        payItemsDTO.setAppId("wxb8239ab824d12860");//wxb8239ab824d12860、12345
        //reqData转json
        String reqData = JsonUtils.toJson(payItemsDTO);
        //System.out.println("reqDatajson= " + reqData);


        //reqData加密
        byte[] bs = DESUtils.encrypt(reqData.getBytes(CHARSET_UTF_8), INTER_KEY);
        String reqDataEncrypt = Base64Utils.encodeToString(bs);
        rm.setReqData(reqDataEncrypt);
        rm.setSign(RSAUtils.sign(reqDataEncrypt, MER_PRIVATE_KEY));

        String reqStr = JsonUtils.toJson(rm);
        //System.out.println("请求数据：" + reqStr);


        String respData = HttpClientHelper.sendPost(OMS_TEST_URL, "application/json", reqStr, "POST", MerInfoConstant.CHARSET_UTF_8);
        //System.out.println("返回数据："+respData);


        return respData;
    }
    @Override
    public Arguments getDefaultParameters() {
        Arguments params = new Arguments();
        params.addArgument("merId",merID+"");//设置参数，企业id
        params.addArgument("merPrivateKey", merPrivateKey);//设置参数，merPrivateKey
        params.addArgument("interKey",interKey);//设置参数，interKey
        return params;
    }

    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {

        SampleResult sampleResult = new SampleResult();
        sampleResult.sampleStart();//开始计时
        LijianjinSendMoneyTest sendMoneyTest = new  LijianjinSendMoneyTest();
        merID = javaSamplerContext.getLongParameter("merId");
        merPrivateKey = javaSamplerContext.getParameter("merPrivateKey");
        interKey = javaSamplerContext.getParameter("interKey");
        try {
            String resData = sendMoneyTest.runTestUserSignQueryService(merID,merPrivateKey,interKey);
            JSONObject jsonObject1 = JSONObject.parseObject(resData);
            if (jsonObject1.get("resCode").equals("0000") || jsonObject1.get("resMsg").toString().contains("频繁")  ) {
                sampleResult.setResponseOK();
                sampleResult.setResponseData(true  + " " + jsonObject1.get("resMsg"), SampleResult.TEXT);
                System.out.println("立减金发放结果:" + true + " " + jsonObject1.get("resMsg"));
            } else {
                sampleResult.setSuccessful(false);
                sampleResult.setResponseData(false + " " + jsonObject1.get("resMsg"), SampleResult.TEXT);
                System.out.println("立减金发放结果:" + false + " " + jsonObject1.get("resMsg")+ " reqId:" + jsonObject1.get("reqId"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            sampleResult.sampleEnd();//结束计时
        }


        return sampleResult;
    }


    //public static void main(String[] agrs){
    //    LijianjinSendMoneyTest sendMoneyTest = new  LijianjinSendMoneyTest();
    //    sendMoneyTest.runTest(new JavaSamplerContext(sendMoneyTest.getDefaultParameters()));
    //
    //}

}
